package infoManage.controller;

import infoManage.domain.Student;
import infoManage.service.StudentService;

import java.util.Scanner;

public class StudentController {
    Scanner sc = new Scanner(System.in);
    StudentService studentService = new StudentService();

    public void start() {
        studentLoop:
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    break;
                case "2":
                    deleteStudent();
                    break;
                case "3":
                    editStudent();
                    break;
                case "4":
                    queryStudent();
                    break;
                case "5":
                    System.out.println("感谢您使用学生管理系统, 再见!");
                    break studentLoop;
                default:
                    System.out.println("您的输入有误，请重新输入");
                    break;
            }
        }
    }

    public void editStudent() { // 修改
        String id = inputStudentId();
        Student stu = inputStudentInfo(id);
        studentService.editStudent(stu);
        System.out.println("修改成功！");
    }

    public void deleteStudent() { // 删除
        Student[] list = studentService.findAllStudent();
        if (list == null) {
            System.out.println("查无信息, 请添加后重试");
            return;
        }
        String id = inputStudentId();
        studentService.deleteStudent(id);
        System.out.println("删除成功！");
    }

    public void queryStudent() { // 查看
        Student[] list = studentService.findAllStudent();
        if (list == null) {
            System.out.println("查无信息, 请添加后重试");
            return;
        }
        System.out.println("学号\t\t姓名\t年龄\t生日");
        for (int i = 0; i < list.length; i++) {
            Student stu = list[i];
            if (stu != null) System.out.println(stu.getId() + "\t" + stu.getName() + "\t" + stu.getAge() + "\t" + stu.getBirthday());
        }
    }

    public void addStudent() { // 添加
        String id;
        while (true) {
            System.out.println("请输入学生的学号：");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag) {
                System.out.println("学号已被占用, 请重新输入");
            } else {
                break;
            }
        }
        Student stu = inputStudentInfo(id);
        // 将学生对象,传递给StudentService(业务员)中的addStudent方法
        boolean result = studentService.addStudent(stu);
        //  根据返回的boolean类型结果, 在控制台打印成功\失败
        if (result) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
    }

    public Student inputStudentInfo(String id) { // 录入学生信息
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生年龄:");
        String age = sc.next();
        System.out.println("请输入学生生日:");
        String bir = sc.next();
        Student stu = new Student(id, name, age, bir);
        return stu;
    }

    public String inputStudentId() { // 判断学生 id
        String id;
        while (true) {
            System.out.println("请输入学生的学号：");
            id = sc.next();
            boolean flag = studentService.isExists(id);
            if (flag) {
                break;
            } else {
                System.out.println("您输入的id不存在, 请重新输入:");
            }
        }
        return id;
    }
}
